package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.generator.entity.FriendsEntity;
import io.renren.modules.generator.service.FriendsService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * HR关系表; InnoDB free: 10240 kB
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-07-14 13:56:25
 */
@RestController
@RequestMapping("generator/friends")
public class FriendsController extends AbstractController {
    @Autowired
    private FriendsService friendsService;

    /**
     * 列表
     */
//    @RequestMapping("/list")
//    @RequiresPermissions("generator:friends:list")
//    public R list(@RequestParam Map<String, Object> params){
//        PageUtils page = friendsService.queryPage(params);
//
//        return R.ok().put("page", page);
//    }
    /*
       好友验证列表
     */
    @RequestMapping("/msgList")
    public R addMsgList(Long rid){
        List list=friendsService.msgList(getUserId());
        return R.ok().put("list",list);
    }

    /*
    好友姓名查询
     */
    @RequestMapping("/friendList")
    public  R friendList(Long id,String username){
        List list=friendsService.getList2(getUserId(),username);
        return R.ok().put("list",list);
    }

    @RequestMapping("/friendList2")
    public  R friendList2(@RequestParam Map<String, Object> params){
//        for (Map.Entry<String, Object> entry : params.entrySet()) {
//            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
//        }


        String username = (String)params.get("username");
        List<Map<String,Object>> list=friendsService.getList2(getUserId(),username);

        PageUtils page = new PageUtils(list, list.size(), Integer.parseInt(params.get("limit").toString()), Integer.parseInt(params.get("page").toString()));

        //return R.ok().put("list",list);
        return R.ok().put("page", page);
    }

    /*
    用户电话号码查询
     */
    @RequestMapping("friendListByMobile")
    public R friendListByMobile(String mobile){
        List list=friendsService.friendListByMobile(mobile);
        return R.ok().put("list",list);
    }

    /*
    修改好友状态
     */
    @RequestMapping("/updateState")
    public R updateState(Long friSender,Long friState){
        friendsService.updateState(friSender,getUserId(),friState);
        return R.ok();
    }

    /*
   拉黑好友
    */
    @RequestMapping("/updateStateByid")
    public R updateStateByid(Long id,Long friState){
        //friendsService.updateState(getUserId(),friReceiver,friState);
        Long uid=getUserId();
        FriendsEntity fri=friendsService.findFriByids(uid,id);
        if(fri!=null){
            friendsService.updateState(uid,id,friState);
        }else {
            friendsService.updateState(id,uid,friState);
        }
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{friId}")
    @RequiresPermissions("generator:friends:info")
    public R info(@PathVariable("friId") Long friId){
		FriendsEntity friends = friendsService.getById(friId);

        return R.ok().put("friends", friends);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("generator:friends:save")
    public R save(@RequestBody FriendsEntity friends){
		friendsService.save(friends);

        return R.ok();
    }
    /*
    好友添加
     */
    @RequestMapping("/add")
    public R add(Long rid,String friMsg){
        List list1=friendsService.getReceiverList(getUserId());//查询你是发送者时你的好友ID
        List list2=friendsService.getSenderList(getUserId());//查询你是接受者时你的好友ID
        for (int i = 0; i <list1.size() ; i++) {
            if(rid==list1.get(i)){
              return R.error("您已添加该用户,请勿重复添加！");
            }
        }
        for (int i = 0; i <list2.size() ; i++) {
            if(rid==list2.get(i)){
                return R.error("您已添加该用户,请勿重复添加！");
            }
        }

        friendsService.add(getUserId(),rid,friMsg);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("generator:friends:update")
    public R update(@RequestBody FriendsEntity friends){
		friendsService.updateById(friends);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("generator:friends:delete")
    public R delete(@RequestBody Long[] friIds){
		friendsService.removeByIds(Arrays.asList(friIds));

        return R.ok();
    }

}
